<?php
//COMANDO UTEIS DO MODELO
//$rowCount=$command->execute();   // Conta resultados retornados (execute the non-query SQL)
//$dataReader=$command->query();   // Retorna objeto da consulta (execute a query SQL)
//$rows=$command->queryAll();      // Retorna um array de dados (query and return all rows of result)
//$row=$command->queryRow();       // Retorna a primeira linha da consulta (query and return the first row of result)
//$column=$command->queryColumn(); // Retorna a primeira coluna da consulta (query and return the first column of result)
//$value=$command->queryScalar();  // Retorna o primeiro campo da primeira linha (query and return the first field in the first row)

/**
 * Classe modelo para tratamento de dados, aqui ficam funcoes de busca ao banco
 */
class MyTest
{
    public $myField1 = NULL;
    public $myField2 = NULL;

    public function buscarDados()
    {
        $sql = "
            select
                * 
            from
                MY_TABLE1 as mt1
                join MY_TABLE2 as mt2 on 
                    mt1.MyField4 = mt2.MyField8
            where
                MyField1 = 1
                and MyField2 = '12/10/1999'
            order by
                MyField1;
        ";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($sql);
        $command->bindParam(':MyField1', $this->myField1, PDO::PARAM_INT);
        $command->bindParam(':MyField2', $this->myField2, PDO::PARAM_STR);
        return $command->queryScalar();
    }
    
    public function excluirDados()
    {
        $sql = "delete from
                    MY_TABLE1
                where
                    MyField1 = :MyField1;";

        $connection = Yii::app()->db;
        $command = $connection->createCommand($sql);
        $command->bindParam(':MyField1', $this->myField1, PDO::PARAM_INT);
        if($command->execute())
            return true;
        else
            return false;
    }
}